<?php
App::uses('AppController', 'Controller');
/**
 * QuestionGroups Controller
 *
 * @property QuestionGroup $QuestionGroup
 */
class QuestionGroupsController extends AppController {

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->QuestionGroup->recursive = 0;
		$this->set('questionGroups', $this->paginate());
	}

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		$this->QuestionGroup->id = $id;
		if (!$this->QuestionGroup->exists()) {
			throw new NotFoundException(__('Invalid data'));
		}
		$this->set('questionGroup', $this->QuestionGroup->read(null, $id));
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
		
		 if(isset($this->request->data["QuestionGroup"]["user_id"])){
		        //$useId =AuthComponent::user('id');		
		        $useId ="1";				
				$this->request->data["QuestionGroup"]["user_id"]=$useId;
				}
				
			$this->QuestionGroup->create();
			if ($this->QuestionGroup->save($this->request->data)) {
				$id=$this->QuestionGroup->id;
				$this->savelog($id);
				$this->Session->setFlash(__('Data has been saved'), 'default',array("class"=>"mws-form-message success"));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default',array("class"=>"mws-form-message error"));
			}
		}
	}

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		$this->QuestionGroup->id = $id;
		if (!$this->QuestionGroup->exists()) {
			throw new NotFoundException(__('Invalid question group'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
		
		 if(isset($this->request->data["QuestionGroup"]["user_id"])){
		       //$useId =AuthComponent::user('id');		
		        $useId ="1";				
				$this->request->data["QuestionGroup"]["user_id"]=$useId;
				}
				
			if ($this->QuestionGroup->save($this->request->data)) {
			      $this->savelog($id);
				$this->Session->setFlash(__('Data has been saved'), 'default',array("class"=>"mws-form-message success"));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default',array("class"=>"mws-form-message error"));
			}
		} else {
			$this->request->data = $this->QuestionGroup->read(null, $id);
		}
	   $this->render("admin_add");
	}

/**
 * admin_delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->QuestionGroup->id = $id;
		if (!$this->QuestionGroup->exists()) {
			throw new NotFoundException(__('Invalid question group'));
		}
		if ($this->QuestionGroup->delete()) {
		       $this->savelog($id);
				
			$this->Session->setFlash(__('Data has been deleted'), 'default',array("class"=>"mws-form-message success"));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Data was not deleted'), array('action' => 'index'), array('action' => 'index'));
		$this->redirect(array('action' => 'index'));
	}
}
